package cn.piesat.client.utils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 读取算法配置文件工具类
 * @author liuyang
 */
public class ReadPropertiesUtil {

	/**
	 * 读取配置文件封装到集合
	 * @param file 文件对象
	 * @return 参数集合
	 * @throws IOException
	 */
	public static Map<String, String> read(File file) throws IOException {
		Map<String, String> map = new HashMap<>();
		if(file.exists()) {
			List<String> strings = FileUtils.readLines(file, StandardCharsets.UTF_8);
			for (String readLine : strings) {
				readLine = readLine.trim();
				if(StringUtils.isBlank(readLine) || readLine.contains("#")){
					continue;
				}
				String[] split = readLine.split("=");
				System.out.println(split[0].toString());
				String key = split[0].trim();
				System.out.println(split[1].toString());
				String value =split[1].trim();
				if(map.containsKey(key)) {
					value = String.format("%s;%s", map.get(key), value);
				}
				map.put(key, value);
			}
		}
		return map;
	}

	public static void main(String[] args) throws IOException {
	  File file = new File("F:\\java\\0629\\SWAPDRTLST.properties");
		read(file);
	}

}
